package cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbErrorManagement;

import cn.iocoder.yudao.framework.common.genId.PKGenerator;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbErrorManagement.vo.*;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.tbErrorManagement.XTbErrorManagementDO;
import cn.iocoder.yudao.module.xiaoBizHandler.convert.tbErrorManagement.XTbErrorManagementConvert;
import cn.iocoder.yudao.module.xiaoBizHandler.service.tbErrorManagement.XTbErrorManagementService;

@Api(tags = "管理后台 - 报错管理")
@RestController
@RequestMapping("/xiaoBizHandler/XT-b-error-management")
@Validated
public class XTbErrorManagementController {

    @Resource
    private XTbErrorManagementService xTbErrorManagementService;

    @PostMapping("/create")
    @ApiOperation("创建报错管理")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-error-management:create')")
    public CommonResult<String> createXTbErrorManagement(@Valid @RequestBody XTbErrorManagementCreateReqVO createReqVO) {
        createReqVO.setId(PKGenerator.newId());
        return success(xTbErrorManagementService.createXTbErrorManagement(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新报错管理")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-error-management:update')")
    public CommonResult<Boolean> updateXTbErrorManagement(@Valid @RequestBody XTbErrorManagementUpdateReqVO updateReqVO) {
        xTbErrorManagementService.updateXTbErrorManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除报错管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-error-management:delete')")
    public CommonResult<Boolean> deleteXTbErrorManagement(@RequestParam("id") String id) {
        xTbErrorManagementService.deleteXTbErrorManagement(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得报错管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-error-management:query')")
    public CommonResult<XTbErrorManagementRespVO> getXTbErrorManagement(@RequestParam("id") String id) {
        XTbErrorManagementDO xTbErrorManagement = xTbErrorManagementService.getXTbErrorManagement(id);
        return success(XTbErrorManagementConvert.INSTANCE.convert(xTbErrorManagement));
    }

    @GetMapping("/list")
    @ApiOperation("获得报错管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-error-management:query')")
    public CommonResult<List<XTbErrorManagementRespVO>> getXTbErrorManagementList(@RequestParam("ids") Collection<String> ids) {
        List<XTbErrorManagementDO> list = xTbErrorManagementService.getXTbErrorManagementList(ids);
        return success(XTbErrorManagementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得报错管理分页")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-error-management:query')")
    public CommonResult<PageResult<XTbErrorManagementRespVO>> getXTbErrorManagementPage(@Valid XTbErrorManagementPageReqVO pageVO) {
        PageResult<XTbErrorManagementDO> pageResult = xTbErrorManagementService.getXTbErrorManagementPage(pageVO);
        return success(XTbErrorManagementConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出报错管理 Excel")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-error-management:export')")
    @OperateLog(type = EXPORT)
    public void exportXTbErrorManagementExcel(@Valid XTbErrorManagementExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<XTbErrorManagementDO> list = xTbErrorManagementService.getXTbErrorManagementList(exportReqVO);
        // 导出 Excel
        List<XTbErrorManagementExcelVO> datas = XTbErrorManagementConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "报错管理.xls", "数据", XTbErrorManagementExcelVO.class, datas);
    }

}
